<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="icon" type="image/x-icon" href="/static/Icons/favicon.ico">
    <title>WebDAV 登录</title>
    <style>
        :root {
            --bg-primary: #121212;
            --bg-secondary: #1e1e1e;
            --text-primary: #e0e0e0;
            --text-secondary: #a0a0a0;
            --accent: #2962ff;
            --accent-hover: #0039cb;
            --accent-bg: rgba(67, 97, 238, 0.1);
            --error: #f72585;
            --error-bg: rgba(247, 37, 133, 0.1);
            --success: #00c853;
        }
        
        * {
            user-select: none;
            -ms-user-select: none;
            -moz-user-select: none;
            -webkit-user-select: none;
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        body {
            background-color: var(--bg-primary);
            color: var(--text-primary);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        
        .login-container {
            background-color: var(--bg-secondary);
            border-radius: 12px;
            padding: 40px;
            width: 100%;
            max-width: 420px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
            animation: fadeIn 0.5s ease-out;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .logo {
            text-align: center;
            margin-bottom: 30px;
        }
        
        .logo h1 {
            font-size: 28px;
            font-weight: 600;
            margin-bottom: 8px;
            background: linear-gradient(90deg, var(--accent), #82b1ff);
            background-clip: text;
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }
        
        .logo p {
            color: var(--text-secondary);
            font-size: 14px;
        }
        
        .form-group {
            margin-bottom: 20px;
        }
        
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-size: 14px;
            color: var(--text-secondary);
        }
        
        .form-group input {
            width: 100%;
            padding: 12px 16px;
            background-color: rgba(255, 255, 255, 0.05);
            border: 1px solid rgba(255, 255, 255, 0.1);
            border-radius: 6px;
            color: var(--text-primary);
            font-size: 16px;
            transition: all 0.3s ease;
        }
        
        .form-group input:focus {
            outline: none;
            border-color: var(--accent);
            box-shadow: 0 0 0 2px rgba(41, 98, 255, 0.2);
        }
        
        .remember-forgot {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 20px;
            font-size: 14px;
        }
        
        .remember {
            display: flex;
            cursor: pointer;
            align-items: center;
        }
        
        .remember input {
            cursor: pointer;
            margin-right: 8px;
        }

        .remember label {
            color: var(--text-primary);
            cursor: pointer;
        }
        
        .remove-password {
            background: var(--accent-bg);
            border: none;
            color: var(--accent);
            cursor: pointer;
            transition: all 0.2s ease;
            display: flex;
            align-items: center;
            padding: 5px 10px;
            border-radius: 4px;
        }
        
        .remove-password:hover {
            color: var(--error);
            background: var(--error-bg);
        }
        
        .submit-btn {
            width: 100%;
            padding: 14px;
            background-color: var(--accent);
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s;
        }
        
        .submit-btn:hover {
            background-color: var(--accent-hover);
        }
        
        .submit-btn:active {
            transform: translateY(1px);
        }
        
        .message {
            margin-top: 20px;
            padding: 12px;
            border-radius: 6px;
            text-align: center;
            font-size: 14px;
            display: none;
        }
        
        .error {
            background-color: rgba(207, 102, 121, 0.2);
            color: var(--error);
            border: 1px solid rgba(207, 102, 121, 0.3);
        }
        
        .success {
            background-color: rgba(0, 200, 83, 0.2);
            color: var(--success);
            border: 1px solid rgba(0, 200, 83, 0.3);
        }
        
        @media (max-width: 420px) {
            .login-container {
                padding: 40px 20px;
            }
        }
    </style>
</head>
<body>
    <div class="login-container">
        <div class="logo">
            <h1>WebDAV 文件管理器</h1>
            <p>登录以访问您的文件</p>
        </div>
        
        <form id="loginForm">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required autocomplete="username">
            </div>
            
            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required autocomplete="current-password">
            </div>
            
            <div class="remember-forgot">
                <div class="remember">
                    <input type="checkbox" id="remember" name="remember">
                    <label for="remember">记住我</label>
                </div>
                <button type="button" class="remove-password" id="remove-password">移除登录凭证</button>
            </div>
            
            <button type="submit" class="submit-btn">登录</button>
        </form>
        
        <div id="message" class="message"></div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const loginForm = document.getElementById('loginForm');
            const messageEl = document.getElementById('message');
            
            // 检查Cookie中是否有保存的登录信息
            const savedUsername = getCookie('webdav_username');
            const savedPassword = getCookie('webdav_password');
            
            if (savedUsername && savedPassword) {
                document.getElementById('username').value = savedUsername;
                document.getElementById('password').value = savedPassword;
                document.getElementById('remember').checked = true;
                
                // 自动尝试登录
                // attemptLogin(savedUsername, savedPassword);
            }

            // 移除登录凭证
            const removePasswordBtn = document.getElementById('remove-password');
            removePasswordBtn.addEventListener('click', function(e) {
                e.preventDefault();
                
                // 清除保存的登录信息
                deleteCookie('webdav_username');
                deleteCookie('webdav_password');
                
                // 重置表单
                document.getElementById('username').value = '';
                document.getElementById('password').value = '';
            });
            
            // 表单提交事件
            loginForm.addEventListener('submit', function(e) {
                e.preventDefault();
                
                const username = document.getElementById('username').value;
                const password = document.getElementById('password').value;
                const remember = document.getElementById('remember').checked;
                
                // 尝试登录
                attemptLogin(username, password, remember);
            });
            
            function attemptLogin(username, password, remember = false) {
                showMessage('正在登录...', 'success');
                
                // 使用Basic Auth进行认证
                const authHeader = 'Basic ' + btoa(username + ':' + password);
                
                fetch('/', {
                    method: 'GET',
                    headers: {
                        'Authorization': authHeader
                    }
                })
                .then(response => {
                    if (response.status === 200) {
                        showMessage('登录成功！正在跳转...', 'success');

                        // 保存登录信息到Cookie（如果用户选择记住我）
                        if (remember) {
                            setCookie('webdav_username', username, 30);
                            setCookie('webdav_password', password, 30);
                        } else {
                            // 清除保存的登录信息
                            deleteCookie('webdav_username');
                            deleteCookie('webdav_password');
                        }
                        
                        setTimeout(() => {
                            window.location.href = (window.location.pathname + '/').replace('//', '/');
                        }, 500);
                    } else if (response.status === 401) {
                        showMessage('用户名或密码错误', 'error');
                    } else {
                        showMessage('登录失败，请重试', 'error');
                    }
                })
                .catch(error => {
                    console.error('登录错误:', error);
                    showMessage('网络错误，请检查连接', 'error');
                });
            }
            
            function showMessage(text, type) {
                messageEl.textContent = text;
                messageEl.className = 'message ' + type;
                messageEl.style.display = 'block';
                
                // 5秒后自动隐藏消息
                setTimeout(() => {
                    messageEl.style.display = 'none';
                }, 5000);
            }
            
            // Cookie操作函数
            function setCookie(name, value, days) {
                const expires = new Date();
                expires.setTime(expires.getTime() + days * 24 * 60 * 60 * 1000);
                document.cookie = name + '=' + value + ';expires=' + expires.toUTCString() + ';path=/;SameSite=Lax';
            }
            
            function getCookie(name) {
                const nameEQ = name + '=';
                const ca = document.cookie.split(';');
                for (let i = 0; i < ca.length; i++) {
                    let c = ca[i];
                    while (c.charAt(0) === ' ') c = c.substring(1, c.length);
                    if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
                }
                return null;
            }
            
            function deleteCookie(name) {
                document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=/';
            }
        });
    </script>
</body>
</html>